package com.tanhua.server.controller;

import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserInfoController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity findUserInfo(@RequestParam(required = false) Long userID,
                                       @RequestParam(required = false) Long huanxinID) {
        Long userId = null;
        if (userID != null) {
            userId = userID;
        } else if (huanxinID != null) {
            userId = huanxinID;
        } else {
            userId = UserHolder.getUser().getId();
        }
        return userInfoService.findUserInfo(userId);
    }

    /**
     * 保存用户资料
     */
    @PutMapping
    public ResponseEntity saveUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUser().getId());
        return userInfoService.saveUserInfo(userInfo);
    }

    /**
     * 保存用户头像
     */
    @PostMapping("/header")
    public ResponseEntity saveHeader(MultipartFile headPhoto) throws IOException {
        Long userId = UserHolder.getUser().getId();
        return userInfoService.saveHeader(headPhoto, userId);
    }

    /**
     * 用户通用设置读取
     * GET/users/settings
     */
    @GetMapping("/settings")
    public ResponseEntity findSettings() {
        return userInfoService.findSettings();
    }

    /**
     * 设置陌生人问题--保存
     * POST/users/questions
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        String content = (String) map.get("content");
        return userInfoService.saveQuestions(content);
    }

    /**
     * 通知设置-保存
     * POST/users/notifications/setting
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity saveNotifications(@RequestBody Settings settings) {
        return userInfoService.saveNotifications(settings);
    }

    /**
     * 黑名单-翻页列表
     * GET/users/blacklist
     */
    @GetMapping("/blacklist")
    public ResponseEntity blackList(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pagesize) {
        return userInfoService.findBlackList(page, pagesize);
    }

    /**
     * 黑名单-移除
     * DELETE/users/blacklist/:uid
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity remove(@PathVariable("uid") Long blackUserId) {
        return userInfoService.remove(blackUserId);
    }

    /**
     * 修改手机号-发送验证码
     * POST /users/phone/sendVerificationCode
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        return userInfoService.sendVerificationCode();
    }

    /**
     * 修改手机号-校验验证码
     * POST/users/phone/checkVerificationCode
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map map) {
        String verificationCode = (String) map.get("verificationCode");
        return userInfoService.checkVerificationCode(verificationCode);
    }

    /**
     * 修改手机号-保存手机号
     * POST/users/phone
     */
    @PostMapping("/phone")
    public ResponseEntity savePhone(@RequestBody Map map) {
        String phone = (String) map.get("phone");
        return userInfoService.savePhone(phone);
    }

    /**
     * 互相喜欢、喜欢、粉丝-统计
     * GET/users/counts
     */
    @GetMapping("/counts")
    public ResponseEntity userLikeCounts() {
        return userInfoService.userLikeCounts();
    }

    /**
     * 互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
     * GET/users/friends/:type
     * 1 互相关注
     * 2 我关注
     * 3 粉丝
     * 4 谁看过我
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity userLikeList(@PathVariable("type") Integer type,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize) {
        return userInfoService.userLikeList(page, pagesize, type);
    }

    /**
     * 我的-粉丝-喜欢
     * POST/users/fans/:uid
     */
    @PostMapping("fans/{uid}")
    public ResponseEntity likeFan(@PathVariable("uid") Long targetUserId) {
        return userInfoService.likeFan(targetUserId);
    }

    /**
     * 我的-取消-喜欢
     * DELETE/users/like/:uid
     */
    @DeleteMapping("/like/{uid}")
    public ResponseEntity removeLike(@PathVariable("uid") Long targetUserId) {
        return userInfoService.removeLike(targetUserId);
    }

}
